package com.tianqu.tms.www.permission.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.UserFunctionDto;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.permission.viewhelper.PermissionManageViewHelper;

/**
 * @author zhangxueliang 权限添加结果controller
 */
public class PermissionCreationResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(PermissionCreationResultController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			PermissionManageViewHelper viewhelper = new PermissionManageViewHelper();
			viewhelper = (PermissionManageViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			UserRole userRole = UserRole.getInstance();

			userRole.setUcId(viewhelper.getUserClassId());
			userRole.setRoleName(viewhelper.getRoleName());
			userRole.setRoleStatus(viewhelper.getRoleStatus());
			int role = userRole.findRole(viewhelper.getUserClassId());
			userRole.setRole(role);
			String funcIdStr = viewhelper.getFirststr();
			List userFunList = (List) viewhelper.getUserFunctionList();

			for (int i = 0; i < userFunList.size(); i++) {
				UserFunctionDto userFunc = (UserFunctionDto) userFunList
						.get(i);
				
				if (userFunc.getChecked() == 1 && userFunc.getFuncGrade() == 2) {
					String funcId = userFunc.getFuncId();
					if(i == userFunList.size() - 1){
						userRole.setFuncIds(userRole.getFuncIds()+funcId);
					}else{
						userRole.setFuncIds(userRole.getFuncIds()+funcId+",");
					}
				
				}
				if (userFunc.getFuncGrade() == 1) {
					if (funcIdStr.indexOf(userFunc.getFuncId()) > -1) {
						if(userRole.getFuncIds() != null){
							userRole.setFuncIds(userRole.getFuncIds()+userFunc.getFuncId()+",");
						}else{
							userRole.setFuncIds(userFunc.getFuncId()+",");
						}
						
					}
				}
			}
			userRole.insertRole(userRole);// 用户角色的添加
			// 对用户的权限表操作 如果是默认的话 自动加上用户相对应的权限
			if (viewhelper.getRoleStatus() == 1) {
				// 取出在该class下的用户list
				User user = User.getInstance();
				UserAuth userAuth = UserAuth.getInstance();
				List userList = user
						.findUserByUcId(viewhelper.getUserClassId());
				if (userList != null && userList.size() > 0) {
					for (int m = 0; m < userList.size(); m++) {
						User userR = (User) userList.get(m);
						userAuth.insertUserAuthInfo(userR.getId(), role);
					}
				}
			}
			
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
